/*
 * Copyright (c) 1994-2017, Joyoung Stock CO.LTD.
 * The FWJR Project.
 * All Rights Reserved.
 */

package com.qdsg.ysg.doctor.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import jz.joyoung.robot.util.NetworkUtil;

import com.qdsg.ysg.doctor.R;

import org.jetbrains.annotations.NotNull;


public class BaseDialogFragment extends DialogFragment {
    protected int mScreenWidth;
    private int mScreenHeight;
    private static ProgressDialog progressDialog;
    protected Activity mContext;

    //正常加载
    //onAttach -> onCreate -> onCreateView -> onActivityCreated -> onStart -> onResume
    //正常退出
    //onPause -> onStop -> onDestroyView -> onDestroyView -> onDestroy -> onDetach。
    @Override
    public void onAttach(@NotNull Context context) {
        mContext = (Activity) context;
        super.onAttach( context );
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        DisplayMetrics metric = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics( metric );
        mScreenWidth = metric.widthPixels; // 屏幕宽度（像素）
        mScreenHeight = metric.heightPixels; // 屏幕高度（像素）
        setStyle( STYLE_NORMAL, R.style.common_selected_dialog );
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        show( manager.beginTransaction(), tag, true );
    }

    @Override
    public int show(FragmentTransaction transaction, String tag) {
        return show( transaction, tag, true );
    }

    public int show(FragmentTransaction transaction, String tag, boolean allowStateLoss) {
        transaction.add( this, tag );
        return allowStateLoss ? transaction.commitAllowingStateLoss() : transaction.commit();
    }

    public void showProgressDialog(Activity mActivity) {
        if (NetworkUtil.isNetworkAvailable( mContext )) {
            if (mActivity != null) {
                // 这个函数用来设置 Dialog 周围的颜色。系统默认的是半透明的灰色。值设为0则为完全透明。
                mContext.getWindow().setDimAmount( 3 );
                progressDialog = new ProgressDialog( mContext );
                //设置进度条风格，风格为圆形，旋转的
                progressDialog.setProgressStyle(
                        ProgressDialog.STYLE_SPINNER );
                //设置ProgressDialog 标题
//        progressDialog.setTitle("下载");
                //设置ProgressDialog 提示信息
                progressDialog.setMessage( "加载中..." );
                //设置ProgressDialog 标题图标
//        progressDialog.setIcon(android.R.drawable.btn_star);
                //设置ProgressDialog 的进度条是否不明确
                progressDialog.setIndeterminate( false );
                //设置ProgressDialog 是否可以按退回按键取消
                progressDialog.setCancelable( true );
                progressDialog.setProgressStyle( R.style.loading_dialog );
                // 让ProgressDialog显示
                progressDialog.show();
            }

        }

    }

    public void dismissProgressDialog() {
//        mHandler.postDelayed( sRunnable, 1000 );
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        dismissProgressDialog();
    }

}
